 <template>
   <div class="right-container">
      <div class="right-box" v-for="(item,index) in nodeInfoData" :key="index">
        <TechBox>
        <NodeInfo v-bind="item" />
        </TechBox>
      </div>
   </div>
 </template>
 
 <script lang="ts">
 import { defineComponent } from 'vue'
 export default defineComponent({
   name: 'Right',
 })
 </script>
 <script setup  lang="ts">
import {nodeManagementApi} from '@/api/nodeManagement'
import dayjs from 'dayjs'
import {getWebSocketInstance} from '@/utils/websocketExample'
const nodeInfoData:any = ref([])
async function handleGetOnlineNodeList(){
  const {data} = await nodeManagementApi.getOnlineNodeList({})
  let list = data.slice(0,3)
  list.forEach((item:any)=>{
    let nodeData = {
      id:0,
      ipAddress:'',
      type:0,
      createTime:'',
      cpu:0,
      disk:0,
      memory:0,
      deployTotal:0,
    }
    nodeData.id = item.nodeId
    nodeData.ipAddress = item.ipAddress
    nodeData.type = item.type
    nodeData.createTime = dayjs(item.createTime).format('YYYY-MM-DD HH:mm:ss')
    nodeData.deployTotal = item.deployCount
    nodeInfoData.value.push(nodeData)
  })

}
const fetchTasks = [
  {fn:handleGetOnlineNodeList,name:"获取在线节点,按部署物数量从大到小排序"}
]
async function init(){
  const results = await Promise.allSettled(
    fetchTasks.map(task => task.fn())
  )  
  results.forEach((result, index) => {
    if (result.status === 'rejected') {
      console.error(`获取${fetchTasks[index].name}失败:`, result.reason)
    }
  })
}
let wsClient = getWebSocketInstance()
const wsResponseHandler:any = {
  "JRLC.get.cpu":handleGetCpu,
}
function handleGetCpu(data:any){
  if(nodeInfoData.value.length === 0 || !nodeInfoData.value.find((item:any)=>item.ipAddress === data.ipAddress)) return
 nodeInfoData.value.forEach((item:any)=>{
  if(item.ipAddress === data.ipAddress){
    item.cpu = parseFloat(data.cpu).toFixed(2)
    item.disk = parseFloat(data.meu).toFixed(2)
    item.memory = Math.floor(Math.random() * 100)
  }
 })
}
wsClient.on("message",(responseData:any)=>{
  const {data,cmd} = responseData
  if(!Object.keys(wsResponseHandler).includes(cmd)) return
  wsResponseHandler[cmd](data)
})
onMounted(()=>{
  init()
})
 </script>
 
 <style  lang="scss" scoped>
.right-container{
  width: 100%;
  height: 100%;
  padding-right: 10px;
  .right-box{
    width: 100%;
    height: 308px;
    // background: rgba(0,10,10,0.5);
    margin-bottom: 10px;
  }
}
</style>
 